package sample.panel;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

import sample.dao.JDO;
import sample.dao.LowLevelAPI;
import sample.dto.Child;
import sample.dto.Parent;

@SuppressWarnings("serial")
public class ReadPanel extends Panel{

	public ReadPanel(String WicketID,final PageParameters pram){
		super(WicketID);
		
		final int UseDao = pram.getInt("Dao");
		
		//リスト化して出力
		List<Parent> entities= new ArrayList<Parent>();
		if(UseDao == 0){
			JDO dao = new JDO();
			entities= dao.getEntityGroup();
		}else{
			LowLevelAPI LowLevelDao = new LowLevelAPI();
			entities = LowLevelDao.getParentList();
		}

		//親要素のリスト
		ListView<Parent> ParentList=new ListView<Parent>("ParentList",entities){
			protected void populateItem(ListItem<Parent> pentity){
				Parent parententity=pentity.getModelObject();
				pentity.add(new Label("Parent",new Model<String>(parententity.getKey().getName()+"(Key)-"+parententity.getValue()+"-(Value)")));
				List<Child> entities = new ArrayList<Child>();
				if(UseDao == 0){
					long time=System.currentTimeMillis();
					entities= parententity.getChildList();
					System.out.println("JDO-time:"+(System.currentTimeMillis()-time)+"miils");
				}else{
					LowLevelAPI LowLevelDao = new LowLevelAPI();
					long time=System.currentTimeMillis();
					entities= LowLevelDao.getChildList(parententity.getKey());
					System.out.println("LowLevel-time:"+(System.currentTimeMillis()-time)+"miils");
				}
				//子要素のリスト
				ListView<Child> ChildList = new ListView<Child>("ChildList",entities) {
					@Override
					protected void populateItem(ListItem<Child> item) {
						Child centity=item.getModelObject();
						item.add(new Label("Child",centity.getChildKey().getName()+"(Key)-"+centity.getChildValue1()+"(Value1)-"+centity.getChildValue2()+"(Value2)"));
					}
				};
				pentity.add(ChildList);
			}
		};
		add(ParentList);
	}
}
